package cn.tedu.qxhouserent.house.controller;

import cn.tedu.qxhouserent.house.pojo.param.NewsDetailAddNewParam;
import cn.tedu.qxhouserent.house.pojo.param.NewsDetailUpdateInfoParam;
import cn.tedu.qxhouserent.house.pojo.vo.NewsDetailListItemVO;
import cn.tedu.qxhouserent.house.pojo.vo.NewsDetailStandardVO;
import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.house.service.NewsDetailService;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@Api(tags = "新闻详情管理模块")
@RestController
@RequestMapping("/newsDetail")
@Validated
public class NewsDetailController {
    @Autowired
    NewsDetailService newsDetailService;


    @ApiOperation("添加新闻详情")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/news/add-new')")
    public JsonResult addNew(@Valid NewsDetailAddNewParam newsDetailAddNewParam) {
        log.debug("controller接收到请求");
        newsDetailService.addNew(newsDetailAddNewParam);
        log.debug("响应给客户端！");
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除新闻详情")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/pms/news/delete')")
    public JsonResult deleteById(@Range(min = 1,message = "根据ID删除新闻详情失败，请提交合法的ID值！")
                                 @RequestParam Long id) {
        log.debug("开始处理【删除新闻详情】的请求，参数：{}", id);
        newsDetailService.deleteById(id);
        log.debug("处理【修改新闻详情】的请求，完成！");
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改新闻详情")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/pms/news/update')")
    public JsonResult updateById(@Range(min = 1,message = "根据ID修改新闻详情失败，请提交合法的ID值！")
                                 @RequestParam Long id,
                                 @Valid NewsDetailUpdateInfoParam newsDetailUpdateInfoParam) {
        log.debug("开始处理【修改新闻详情】的请求，参数：{},{}", id, newsDetailUpdateInfoParam);
        newsDetailService.updateById(id, newsDetailUpdateInfoParam);
        log.debug("处理【修改新闻详情】的请求，完成！");
        return JsonResult.ok();
    }



    @GetMapping("/list")
    @ApiOperation("查询资讯详情列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询咨询详情列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询咨询详情列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<NewsDetailListItemVO> pageData = newsDetailService.list(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询资讯详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "咨询详情ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询咨询详情详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询咨询详情详情】的请求，参数：{}", id);
        NewsDetailStandardVO result = newsDetailService.getStandardById(id);
        return JsonResult.ok(result);
    }
}
